﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Foundation
{
    /// <summary>
    /// 表示一个点
    /// </summary>
    [DebuggerDisplay("x:{X},y:{Y}")]
    public struct Point:IEqualityComparer<Point>
    {
        public static readonly Point OriginPoint = new Point(0,0);

        public Point(float x, float y)
        {
            X = x;
            Y = y;
        }

        /// <summary>
        /// 获取或设置X坐标值
        /// </summary>
        public float X { get; set; }

        /// <summary>
        /// 获取或设置Y坐标值
        /// </summary>
        public float Y { get; set; }

        #region Override

        /// <summary>
        /// 获取与当前对象等价的字符串形式
        /// </summary>
        /// <returns>返回与当前对象等价的字符串形式</returns>
        public override string ToString()
        {
            return $"<{X},{Y}>";
        }

        /// <summary>
        /// 获取当前对象与指定对象之间的等价性
        /// </summary>
        /// <param name="obj">指定要比较的对象</param>
        /// <returns>如果两个对象之间等价则返回true，否则返回false</returns>
        public override bool Equals(object obj)
        {
            if (obj is Point point) {
                return point.X == this.X && point.Y == this.Y;
            }
            return false;
        }

        /// <summary>
        /// 获取当前对象的哈希值
        /// </summary>
        /// <returns>返回当前对象的哈希值</returns>
        public override int GetHashCode()
        {
            return X.GetHashCode() ^ Y.GetHashCode() + (int)X;
        }
        #endregion

        #region IEqualityComparer<Point>接口成员

        public bool Equals(Point x, Point y)
        {
            return x == y;
        }

        public int GetHashCode(Point obj)
        {
            return obj.GetHashCode();
        }
        #endregion

        #region operator == / !=

        public static bool operator !=(Point x, Point y)
        {
            return x.X != y.X || x.Y != y.Y;
        }

        public static bool operator ==(Point x, Point y)
        {
            return x.X == y.X && x.Y == y.Y;
        }

        #endregion

        #region operator + / -

        public static Point operator +(Point x,Point y)
        {
            x.X += y.X;
            x.Y += y.Y;
            return x;
        }

        public static Point operator +(Point x,Size y)
        {
            x.X += y.Width;
            x.Y += y.Height;
            return x;
        }

        public static Point operator -(Point x, Point y)
        {
            x.X -= y.X;
            x.Y -= y.Y;
            return x;
        }

        public static Point operator -(Point x, Size y)
        {
            x.X -= y.Width;
            x.Y -= y.Height;
            return x;
        }

        #endregion
    }
}
